package org.huawei.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.huawei.dao.UserDao;
import org.huawei.model.domain.User;
import org.huawei.annotations.RateLimit;
import org.huawei.view.AlartView;
import org.huawei.view.SuccessView;
import org.huawei.view.MainView;
import javafx.stage.Stage;

import java.sql.SQLException;
import java.util.List;

public class UserController {
    private Stage stage;
    private UserDao userDao;

    public UserController(Stage stage) {
        this.stage = stage;
        this.userDao = new UserDao();
    }

    @RateLimit
    public void trylogin(String userAccount, String userPassword) {
        try {
            // 去掉前后空格
            userAccount = userAccount.trim();
            userPassword = userPassword.trim();
            // 检查账号是否为空
            if (userAccount == null || userAccount.isEmpty()) {
                AlartView.alart(stage, "账号不能为空！");
                return;
            }
            // 检查密码是否为空
            if (userPassword == null || userPassword.isEmpty()) {
                AlartView.alart(stage, "密码不能为空！");
                return;
            }
            User user = new User(userAccount, userPassword);
            User currUser = userDao.login(user);
            if (currUser != null) {
                MainView mainView = new MainView();
                mainView.start(stage,currUser);
            } else {
                AlartView.alart(stage, "账号或密码不正确！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AlartView.alart(stage, "登录过程中出现错误！");
        }
    }

    @RateLimit
    public void tryResetPassword(String password_1, String password_2, User currentUser,Stage resetPasswordStage) {
        if (password_1 == null || password_1.isEmpty()) {
            AlartView.alart(stage, "密码不能为空！");
            return;
        }
        if (password_2 == null || password_2.isEmpty()) {
            AlartView.alart(stage, "密码不能为空！");
            return;
        }
        if(password_1.equals(password_2)){
            currentUser.setUserPassword(password_1);
            try {
                userDao.updateUserPassword(currentUser);
                SuccessView.success(stage,"密码修改成功！");
                resetPasswordStage.close();
            } catch (Exception e) {
                e.printStackTrace();
                AlartView.alart(stage, "密码修改失败！");
            }
        }else{
            AlartView.alart(stage, "两次密码不一致！");
        }
    }

    @RateLimit
    public ObservableList<User> getAllUser() throws SQLException {
        // 获取用户列表
        List<User> userList = userDao.getAllUsers();

        // 将List转换为ObservableList
        return FXCollections.observableArrayList(userList);
    }

    @RateLimit
    public void updateUserInDatabase(User user) {
        try {
            userDao.updateUserInDatabase(user);
        } catch (SQLException e) {
            e.printStackTrace();
            AlartView.alart(stage, "错误！");
        }
    }

    @RateLimit
    public List<User> getUsersByRoleId(int roleId) throws SQLException {
        return userDao.getUsersByRoleId(roleId);
    }
}
